﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using EnergyConsumptionMonitorSystem.Components;
using EnergyConsumptionMonitorSystem.Models;
using System.Windows;
using System.Security;
using System.Windows.Controls;
using System.Runtime.InteropServices;

namespace EnergyConsumptionMonitorSystem.MainWindowTabItem
{
    public class LogInUCViewModel : TabItemContentUCViewModel
    {
        public event EventHandler RegisterEvent;

        private string _userName;
        public string UserName
        {
            get { return this._userName; }
            set
            {
                if (this._userName != value)
                {
                    this._userName = value;
                    NotifyPropertyChanged("UserName");
                }
            }
        }

        private ICommand _registerCommand;
        public ICommand RegisterCommand { get { return this._registerCommand; } }

        private ICommand _logInCommand;
        public ICommand LogInCommand { get { return this._logInCommand; } }

        public delegate void HarvestPasswordHandler(object sender,PasswordEventArgs e);
        public event HarvestPasswordHandler HarvestPasswordEvent;

        public LogInUCViewModel()
        {
            this._view = new LogInUC(this);
            this._view.DataContext = this;

            this._registerCommand = new Command(new Action<object>(this.Register));
            this._logInCommand = new Command(new Action<object>(this.LogIn));
        }

        private void Register(object o)
        {
            if (this.RegisterEvent != null)
            {
                this.RegisterEvent(this, new EventArgs());
            }
        }

        private void LogIn(object o)
        {
            if (this.HarvestPasswordEvent != null)
            {
                PasswordEventArgs e = new PasswordEventArgs();
                this.HarvestPasswordEvent(this, e);
                string password = ConvertToUnsecureString(e.Password);
                User user = Singleton<DBManager>.Instance.GetUser(this.UserName, password);
                if (user == null)
                {
                    MessageBox.Show("用户名或密码错误！", "登录失败");
                    NotifyPropertyChanged("Password");
                }
                else
                {
                    Singleton<LogInStatus>.Instance.LogIn(user);
                }
            }          
        }

        private string ConvertToUnsecureString(SecureString securePassword)
        {
            if (securePassword == null)
                throw new ArgumentNullException("securePassword");

            IntPtr unmanagedString = IntPtr.Zero;
            try
            {
                unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(securePassword);
                return Marshal.PtrToStringUni(unmanagedString);
            }
            finally
            {
                Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
            }
        }

        protected override void tabItem_ClosingEvent(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = false;
        }
    }
}
